package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.PurchaseProductDao;
import com.xbongbong.saas.domain.entity.BusinessProductEntity;
import com.xbongbong.saas.domain.entity.PurchaseProductEntity;
import com.xbongbong.saas.model.PurchaseProductModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangJian
 * @date  2019-02-21 15:38
 **/
@Service("PurchaseProductModel")
public class PurchaseProductModelImpl implements PurchaseProductModel{
    @Resource
    private PurchaseProductDao purchaseProductDao;
    /**
     * 插入数据
     *
     * @param purchaseProduct 采购合同产品实体数据
     * @return 是否成功
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     **/
    @Override
    public Integer insert(PurchaseProductEntity purchaseProduct) {
        purchaseProduct.setAddTime(DateTimeUtil.getInt());
        purchaseProduct.setUpdateTime(DateTimeUtil.getInt());
        StandardizeDataUtil.standardizeNotString(purchaseProduct);
        return purchaseProductDao.insert(purchaseProduct);
    }

    @Override
    public Integer insertBatch(List<PurchaseProductEntity> list) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        list.forEach(item->{
            item.setAddTime(now);
            item.setUpdateTime(now);
        });
        return purchaseProductDao.insertBatch(list);
    }

    /**
     * 更新数据
     *
     * @param purchaseProduct 采购合同产品实体数据
     * @return 是否成功
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     **/
    @Override
    public Integer update(PurchaseProductEntity purchaseProduct) {
        purchaseProduct.setUpdateTime(DateTimeUtil.getInt());
        StandardizeDataUtil.standardizeNotString(purchaseProduct);
        return purchaseProductDao.update(purchaseProduct);
    }

    /**
     * 根据主键删除记录
     *
     * @param id     主键
     * @param corpid 公司id
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer deleteByKey(Long id, String corpid) {
        return purchaseProductDao.deleteByKey(id,corpid);
    }

    /**
     * 删除采购合同下的所有产品
     *
     * @param purchaseSheetId id集合
     * @param corpid          公司id
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer deleteByPurchaseId(Long purchaseSheetId, String corpid) {
        return purchaseProductDao.deleteByPurchaseId(purchaseSheetId,corpid);
    }

    /**
     * 删除多条采购合同下的所有产品
     *
     * @param purchaseSheetIdIn id集合
     * @param corpid            公司id
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer deleteByPurchaseIdIn(List<Long> purchaseSheetIdIn, String corpid) {
        return purchaseProductDao.deleteByPurchaseIdIn(purchaseSheetIdIn,corpid);
    }

    /**
     * 获取一条采购合同产品数据
     *
     * @param id     采购合同产品ID
     * @param corpid 公司id
     * @return PurchaseProductEntity 一条数据实体
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public PurchaseProductEntity getByKey(Long id, String corpid) {
        return purchaseProductDao.getByKey(id,corpid);
    }

    /**
     * 多参数查询多条数据
     *
     * @param param 参数集合
     * @return List<PurchaseProductEntity> 数据集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public List<PurchaseProductEntity> findEntitys(Map<String, Object> param) {
        return purchaseProductDao.findEntitys(param);
    }

    /**
     * 获取采购合同ID集合
     *
     * @param param 参数集合
     * @return List<Long> Id集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public List<Long> findPurchaseIds(Map<String, Object> param) {
        return purchaseProductDao.findPurchaseIds(param);
    }

    /**
     * 获取记录数
     *
     * @param param 参数集合
     * @return List<Long> Id集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return purchaseProductDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 根据参数集合获取所有的采购合同产品
     *
     * @param param 参数集合
     * @return List<Long> Id集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public List<PurchaseProductEntity> getAllByPurchaseId(Map<String, Object> param) {
        return purchaseProductDao.getAllByPurchaseId(param);
    }

    /**
     * 关联查询
     *
     * @param param 参数集合
     * @return List<Long> Id集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public List<PurchaseProductEntity> findEntitysJoinProduct(Map<String, Object> param) {
        return purchaseProductDao.findEntitysJoinProduct(param);
    }

    /**
     * 获取采购合同关联产品
     *
     * @param corpid      公司id
     * @param purchaseId  采购合同id
     * @return 采购合同产品列表
     * @author Jian.Wang
     * @date 2019/2/27 20:26
     */
    @Override
    public List<PurchaseProductEntity> getProductsByPurchaseId(String corpid, Long purchaseId) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("purchaseSheetId", purchaseId);
        param.put("del", 0);
        param.put(ParameterConstant.ORDER_BY_STR, StringConstant.SAAS_LINK_BUSINESS_ID);

        return findEntitys(param);
    }

    @Override
    public List<PurchaseProductEntity> getPurchaseByContract(String corpid, List<Long> contractIdList) {
        if (CollectionsUtil.isEmpty(contractIdList)) {
            return new ArrayList<>();
        }
        return purchaseProductDao.getPurchaseByContract(corpid,contractIdList);
    }


    @Override
    public List<PurchaseProductEntity> getProductsByPurchaseId(String corpid, Long purchaseId, List<Integer> delIn) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("purchaseSheetId", purchaseId);
        param.put("delIn", delIn);
        param.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        return findEntitys(param);
    }

    /**
     * 获取产品合同根据合同id
     *
     * @param corpid       公司id
     * @param purchaseIdIn 采购合同id
     * @return 数据
     */
    @Override
    public List<PurchaseProductEntity> getPurchaseProductByPurchaseIdIn(String corpid, List<Long> purchaseIdIn) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("purchaseIdIn", purchaseIdIn);
        param.put("del", DelEnum.NORMAL.getDel());
        param.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        return findEntitys(param);
    }

    @Override
    public List<BusinessProductEntity> findProductNum(Map<String, Object> param) {
        return purchaseProductDao.findProductNum(param);
    }

    @Override
    public Integer findProductNumCount(Map<String, Object> param) {
        return purchaseProductDao.findProductNumCount(param);
    }

    @Override
    public List<BusinessProductEntity> findProductNumIncludeProcess(Map<String, Object> param) {
        return purchaseProductDao.findProductNumIncludeProcess(param);
    }

    @Override
    public Integer findProductNumCountIncludeProcess(Map<String, Object> param) {
        return purchaseProductDao.findProductNumCountIncludeProcess(param);
    }

    @Override
    public List<BusinessProductEntity> getPurchasePrice(Long purchaseSheetId, String corpid) {
        return purchaseProductDao.getPurchasePrice(purchaseSheetId,corpid);
    }

    @Override
    public Integer updateBatch(List<PurchaseProductEntity> updateList, String corpid) throws XbbException {
        StandardizeDataUtil.standardize4PurchaseProductEntityList(updateList);
        updateList.forEach(item->{
            item.setUpdateTime(DateTimeUtil.getInt());
        });
        return purchaseProductDao.updateBatch(updateList,corpid);
    }

    @Override
    public Integer updateOutStock(List<PurchaseProductEntity> updateList, String corpid) throws XbbException {
        updateList.forEach(item->{
            item.setUpdateTime(DateTimeUtil.getInt());
        });
        return  purchaseProductDao.updateOutStock(updateList,corpid);
    }

    @Override
    public Integer updateInstock(List<PurchaseProductEntity> updateList, String corpid) throws XbbException {
        updateList.forEach(item->{
            item.setUpdateTime(DateTimeUtil.getInt());
        });
        return  purchaseProductDao.updateInStock(updateList,corpid);
    }

    @Override
    public Long isProductSubAttrExist(String attr, String corpid, Integer isArray) {
        return purchaseProductDao.isProductSubAttrExist(attr, corpid, isArray);
    }

    @Override
    public Double summaryByAttr(String field,String corpid, Long refId, List<Long> productIdIn) {
        return purchaseProductDao.summaryByAttr( field, corpid,refId, productIdIn);
    }
    @Override
    public List<String> getCorpidList() {
        return purchaseProductDao.getCorpidList();

    }

    @Override
    public Double summaryByAttrFromOutSide(String field, String corpid, Long refId, List<Long> productIdIn) {
        return purchaseProductDao.summaryByAttrFromOutSide( field, corpid,refId, productIdIn);
    }

    @Override
    public Double calcuateByAttrFromOutSide(String field1, String field2, String corpid, Long refId, List<Long> productIdIn) {
        return purchaseProductDao.calcuateByAttrFromOutSide( field1, field2, corpid,refId, productIdIn);
    }

    @Override
    public Long countProductSub(String attr, String corpid, Integer isArray) {
        return purchaseProductDao.countProductSub(attr, corpid, isArray);
    }

    @Override
    public List<PurchaseProductEntity> getProductWithSubData(String attr, String corpid, Integer isArray, Long idGte, Integer limit) {
        return purchaseProductDao.getProductWithSubData(attr, corpid, isArray, idGte, limit);
    }

    @Override
    public void updateOutStockForJob(List<PurchaseProductEntity> updateList, String corpid) {
        long now = DateTimeUtil.getInt();
        updateList.forEach(item->{
            item.setUpdateTime(now);
        });
        purchaseProductDao.updateOutStockForJob(updateList,corpid);
    }

    @Override
    public Integer deleteBatch(List<Long> productList, String corpid, Integer del) {
        if (CollectionsUtil.isEmpty(productList)) {
            return 0;
        }
        return purchaseProductDao.deleteBatch(productList, corpid, del);
    }

    @Override
    public Integer updateMulti(List<PurchaseProductEntity> list, String corpid) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        list.forEach(item->{
            item.setUpdateTime(now);
        });
        StandardizeDataUtil.standardize4PurchaseProductEntityList(list);
        return purchaseProductDao.updateMulti(list, corpid);
    }

    @Override
    public List<PurchaseProductEntity> findProductEntitys(Map<String, Object> param){
        return purchaseProductDao.findProductEntitys(param);
    }

    @Override
    public List<PurchaseProductEntity> findIdByPurchaseAndProduct(Map<Long, List<Long>> sheetId, String corpid){
        return purchaseProductDao.findIdByPurchaseAndProduct(sheetId, corpid);
    }

    @Override
    public List<PurchaseProductEntity> getContractPurchase(String corpid , Long dataId){
        return purchaseProductDao.getContractPurchase(corpid , dataId);
    }

    /**
     * 获取记录数
     *
     * @param param 参数集合
     * @return List<Long> Id集合
     * @author jiping.yang
     * @date 2021/9/17 15:18
     */
    @Override
    public Integer getPurchaseProductCount(Map<String, Object> param) {
        return purchaseProductDao.getPurchaseProductCount(param);
    }
}
